package com.java.nio.zerocopy;

import java.io.FileInputStream;
import java.net.InetSocketAddress;
import java.nio.channels.FileChannel;
import java.nio.channels.SocketChannel;

/**
 * @author jl
 * Created on 2020/9/28
 */
public class NewIOClient {

    public static void main(String[] args) throws Exception {
        SocketChannel socketChannel = SocketChannel.open();
        socketChannel.connect(new InetSocketAddress("127.0.0.1", 7000));
        String fileName = "aa.zip";

        // 创建fileChannel
        FileChannel fileChannel = new FileInputStream(fileName).getChannel();
        // 准备发送
        long start = System.currentTimeMillis();
        // 在linux中,一个transferTo方法就可以完成传输
        // 在windows中,一次transferTo只能发送8m
        // transferTo底层是零拷贝
        long count = fileChannel.transferTo(0, fileChannel.size(), socketChannel);

        System.out.println("发送的总字节数=" + count + " 耗时:" + (System.currentTimeMillis() - start));

    }
}
